<?php
namespace app\index\model;

use think\Model;
use think\Db;

class YingMu extends Model
{

/**
 * 注册时同步会员数据到影沐
 * @param $member_info
 * @author hey_heyang
 */
    public function insertYingMuMember($member_info) {
        if(empty($member_info)) return false;
        $data['hry_member_id'] = $member_info['id'];
        $data['mobile'] = $member_info['mobile'];
//        $data['username'] = empty($member_info['username'])?'hry_'.$member_info['id']:$member_info['username'];
        $data['username'] = 'hry_'.$member_info['id'];
        $data['password'] = $member_info['password'];
        $data['created_at'] = time();
        $data['updated_at'] = time();
        try{
            $model=Db::connect('database_ym');
            $ym_member_info=$model->table('member')->where(['mobile'=>$member_info['mobile']])->find();
            if(empty($ym_member_info)){
                $model->table('member')->insert($data);
            }
            return true;
        }catch (\Exception $e){
            file_put_contents(BASE_PATH.'log/'.date("Y-m-d H", time()).'yingmu_member_error.txt',str_replace("\\", "", json_encode($data,JSON_UNESCAPED_UNICODE)).PHP_EOL,FILE_APPEND);
            return true;
        }

    }


    /**
     * 新建项目时同步数据到影沐
     * @param $data
     * @author hey_heyang
     */
    public function insertYingMuProject($data) {
        if(empty($data)) return false;
        try{
            $model=Db::connect('database_ym');
            $ym_member_info=$model->table('project')->where(['id'=>$data['id']])->find();
            if(empty($ym_member_info)){
                $data['start_time'] = strtotime($data['start_time']);
                $data['end_time']= strtotime($data['end_time']);
                $data['release_time']= strtotime($data['release_time']);
                $data['create_time']= strtotime($data['create_time']);
                $data['update_time']= strtotime($data['update_time']);
                $data['tags']= serialize($data['tags']);
                $model->table('project')->insert($data);
            }
            return true;
        }catch (\Exception $e){
            file_put_contents(BASE_PATH.'log/'.date("Y-m-d H", time()).'yingmu_project_error.txt',str_replace("\\", "", json_encode($data,JSON_UNESCAPED_UNICODE)).PHP_EOL,FILE_APPEND);
            return true;
        }

    }


    /**
     * 更新项目状态
     * @param $data
     * @author hey_heyang
     */
    public function updateYingMuProject($data) {
        if(empty($data)) return false;
        try{
            $model=Db::connect('database_ym');
            $ym_member_info=$model->table('project')->where(['id'=>$data['id']])->find();
            if(empty($ym_member_info)){
                $data['start_time'] = strtotime($data['start_time']);
                $data['end_time']= strtotime($data['end_time']);
                $data['release_time']= strtotime($data['release_time']);
                $data['create_time']= strtotime($data['create_time']);
                $data['update_time']= strtotime($data['update_time']);
                $data['tags']= serialize($data['tags']);
                $data['status']=2;
                $model->table('project')->insert($data);
            }else{
                $update_data['status']=2;
                $model->table('project')->where(['id'=>$data['id']])->update($update_data);
        }
            return true;
        }catch (\Exception $e){
            file_put_contents(BASE_PATH.'log/'.date("Y-m-d H", time()).'yingmu_project_error.txt',str_replace("\\", "", json_encode($data,JSON_UNESCAPED_UNICODE)).PHP_EOL,FILE_APPEND);
            return true;
        }

    }



}